package com.manage.activity;

import com.manage.utils.OAuthConstant;
import com.manage.utils.SinaWeiboPoster;

import weibo4andriod.WeiboException;
import weibo4andriod.http.AccessToken;
import weibo4andriod.http.RequestToken;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShareSinaWeiboActivity extends Activity {
	private String rs1, rs2;
	private TextView tv;
	private Button btnBack, btnReUpload;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setContentView(R.layout.share_sina_weibo);

		SharedPreferences spf = this.getSharedPreferences("data", Activity.MODE_PRIVATE);
		if (spf != null) {
			rs1 = spf.getString("Tocken", null);
			rs2 = spf.getString("TokenSecret", null);
		}

		if (rs1 == null && rs2 == null) {
			Uri uri = this.getIntent().getData();
			try {
				RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();
				AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
				OAuthConstant.getInstance().setAccessToken(accessToken);
				Editor e = getSharedPreferences("data", Activity.MODE_WORLD_WRITEABLE).edit();
				e.putString("Tocken", accessToken.getToken());
				e.putString("TokenSecret", accessToken.getTokenSecret());
				e.commit();
			} catch (WeiboException e) {
				e.printStackTrace();
				// AppUtils.logE(e);
			}
		}
		tv = (TextView) this.findViewById(R.id.photo_share_result);
		btnBack = (Button) this.findViewById(R.id.btn_share_back);
		btnReUpload = (Button) this.findViewById(R.id.btn_share_reupload);
		initLayoutView();
		initButtonListener();
		setPostPhotoUploadResult();

	}

	private void setLayoutView(String result) {
		// TODO Auto-generated method stub
		// String ss = SinaWeiboUtils.result;
		if (result == SinaWeiboPoster.RESULT_FAILED) {
			tv.setText("对不起，您的操作失败，请尝试重新操作或者返回主界面！");
			btnBack.setVisibility(View.VISIBLE);
			btnReUpload.setVisibility(View.VISIBLE);
		} else if (result == SinaWeiboPoster.RESULT_OK) {
			tv.setText("恭喜您，发送微薄成功！");
			btnBack.setVisibility(View.VISIBLE);
			btnReUpload.setVisibility(View.GONE);
		}
	}

	private void initButtonListener() {
		// TODO Auto-generated method stub
		btnBack.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ShareSinaWeiboActivity.this.finish();
			}
		});

		btnReUpload.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				initLayoutView();
				setPostPhotoUploadResult();
			}
		});
	}

	private void setPostPhotoUploadResult() {
		SinaWeiboPoster weiboPoster = new SinaWeiboPoster();
		weiboPoster.setWeiboListener(new SinaWeiboPoster.WeiboListener() {
			public void afterPost(String result) {
				setLayoutView(result);
			}
		});
		weiboPoster.postPhotoUpload(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		initLayoutView();
	}

	private void initLayoutView() {
		// TODO Auto-generated method stub
		if (tv.getText() != null || !tv.getText().equals(""))
			tv.setText("");
		if (btnBack.getVisibility() == View.VISIBLE)
			btnBack.setVisibility(View.GONE);
		if (btnReUpload.getVisibility() == View.VISIBLE)
			btnReUpload.setVisibility(View.GONE);
	}

}
